//
//  MiniCirCleViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/7.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "MiniCirCleViewController.h"
#import "MiniCircleCollectionViewCell.h"
#import "MakeFriendsInfoViewController.h"
#import "PushFriendsViewController.h"
#import "TalksTableViewCell.h"
#import "TalksHeader.h"
#import "TalksInfoViewController.h"
#import "MakeFriendsModel.h"
#import "XiehouModel.h"
#import "XiehouView.h"
#import "TopicModel.h"
#import "WeiteTieZiViewController.h"

@interface MiniCirCleViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UICollectionView * collectionView;
@property (nonatomic, weak)UIView * topView;
@property (nonatomic, strong)UIButton * makeFirendsButton;
@property (nonatomic, strong)UIButton * seeyouButton;
@property (nonatomic, strong)UIButton * talksButton;
@property (nonatomic, strong)UIButton * cameraButton;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, copy)NSString * type;
@property (nonatomic, copy)NSString * typeTopic;
@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<MakeFriendsModel *> * modelListArray;
@property (nonatomic, weak)XiehouView * xiehouView;
@property (nonatomic, strong)NSArray <XiehouModel *> * xiehouListArray;
@property (nonatomic, strong)NSArray <TopicModel *> * topicListModel;

@end

@implementation MiniCirCleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view .backgroundColor = WHITE_COLOR;
    self.type = @"one";
    self.typeTopic = @"new";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToPersonInfoVC:) name:@"pushToPersonInfoVC" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RefreshTalksData:) name:@"RefreshTalksData" object:nil];
    
    [MakeFriendsModel makeFriendsWithSucess:^(NSArray<MakeFriendsModel *> *array) {
        
        self.modelListArray = array;
    } error:^{
        
    }];
    
    [XiehouModel xiehouWithSucess:^(NSArray<XiehouModel *> *array) {
    
        self.xiehouListArray = array;
    } error:^{
        
    }];

    [self loadTopicDataType:self.typeTopic];
    [self setupUI];
}


- (void)setModelListArray:(NSArray<MakeFriendsModel *> *)modelListArray{
    
    _modelListArray = modelListArray;
    
    [self.collectionView reloadData];
}


- (void)setXiehouListArray:(NSArray<XiehouModel *> *)xiehouListArray{
    
    _xiehouListArray = xiehouListArray;
    
    self.xiehouView.modelArray = xiehouListArray;
}


- (void)setTopicListModel:(NSArray<TopicModel *> *)topicListModel{
    
    _topicListModel = topicListModel;
    
    [self.tableView reloadData];
}

- (void)loadTopicDataType:(NSString *)str{
    
    [TopicModel topicModelWithType:str WithSucess:^(NSArray<TopicModel *> *array) {
       
        self.topicListModel = array;
    } error:^{
        
    }];
}

- (void)setupUI{
    
    [self setupTopiView];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 1.设置列间距
    layout.minimumInteritemSpacing = 7 * SCREENH6;
    // 2.设置行间距
    layout.minimumLineSpacing = 10 * SCREENH6;
    // 3.设置每个item的大小
    layout.itemSize = CGSizeMake(171 * SCREENH6, 280 * SCREENH6);
    // 5.设置布局方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(13 * SCREENH6, 45 * SCREENH6, SCREEN_WIDTH - 26 * SCREENH6, SCREENH_HEIGHT - 165 * SCREENH6) collectionViewLayout:layout];
    
    
    collectionView.dataSource=self;
    collectionView.delegate=self;
    [collectionView setBackgroundColor:WHITE_COLOR];
    //注册Cell，必须要有
    [collectionView registerClass:[MiniCircleCollectionViewCell class] forCellWithReuseIdentifier:[MiniCircleCollectionViewCell cellIdentifier]];
    
    self.collectionView = collectionView;
    
    [self.view addSubview:self.collectionView];

    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 45 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT - 165 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 230 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.hidden = YES;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[TalksTableViewCell class] forCellReuseIdentifier:@"TalksListCell"];
    [self.tableView registerClass:[TalksHeader class] forHeaderFooterViewReuseIdentifier:@"TalksHeaderView"];
    
    [self.view addSubview:self.tableView];
    
    
//    XiehouView * xiehouView = [[XiehouView alloc] initWithFrame:CGRectMake(0, 45 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT - 165 * SCREENH6)];
//
//    xiehouView.hidden = YES;
//
//    self.xiehouView = xiehouView;
//
//    [self.view addSubview:self.xiehouView];
}



//设置分区数（必须实现）
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

//设置每个分区的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
//    NSLog(@"===%lu",self.modelListArray.count);
    return self.modelListArray.count;
}

//设置返回每个item的属性必须实现）
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MiniCircleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[MiniCircleCollectionViewCell cellIdentifier] forIndexPath:indexPath];
    
    cell.model = self.modelListArray[indexPath.row];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    MakeFriendsModel * model = self.modelListArray[indexPath.row];
    
    MakeFriendsInfoViewController * infoVC = [MakeFriendsInfoViewController new];
    
    infoVC.ID = model.ID;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
    SHOW_TAB;
}


- (void)pushToPersonInfoVC:(NSNotification *)noti{
    
    HIDDEN_TAB;
    
    NSString * ID = noti.userInfo[@"ID"];
    
    MakeFriendsInfoViewController * infoVC = [MakeFriendsInfoViewController new];
    
    infoVC.ID = ID;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
    SHOW_TAB;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.topicListModel.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TalksTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TalksListCell" forIndexPath:indexPath];
    
    cell.model = self.topicListModel[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 45 * SCREENH6;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   
    TalksHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"TalksHeaderView"];
    
    return header;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    TopicModel * model = self.topicListModel[indexPath.row];
    
    TalksInfoViewController * infoVC = [TalksInfoViewController new];
    
    infoVC.ID = model.ID;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
    SHOW_TAB;
}


- (void)setupTopiView{
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = WHITE_COLOR;
    topView.userInteractionEnabled = YES;
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    self.makeFirendsButton = [self setupButtonWith:@"交友"];
    [self.topView addSubview:self.makeFirendsButton];
    
    self.seeyouButton = [self setupButtonWith:@"邂逅"];
    [self.topView addSubview:self.seeyouButton];
    
    self.talksButton = [self setupButtonWith:@"话题"];
    [self.topView addSubview:self.talksButton];
    
    self.oneLineView = [self setupLineView];
    self.oneLineView.hidden = NO;
    [self.topView addSubview:self.oneLineView];
    
    self.twoLineView = [self setupLineView];
    [self.topView addSubview:self.twoLineView];
    
    self.threeLineView = [self setupLineView];
    [self.topView addSubview:self.threeLineView];
    
    UIButton * cameraButton = [UIButton new];
    
    [cameraButton setImage:ImageNamed(@"相机") forState:UIControlStateNormal];
    [cameraButton addTarget:self action:@selector(clickCameraButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.cameraButton = cameraButton;
    
    [self.topView addSubview:self.cameraButton];
}

- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(45 * SCREENH6);
        make.top.leading.trailing.mas_equalTo(self.view);
    }];
    
    [self.seeyouButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.topView);
        make.centerX.mas_equalTo(self.view);
    }];
    
    [self.makeFirendsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.topView);
        make.trailing.mas_equalTo(self.seeyouButton.mas_leading).offset(-50 * SCREENH6);
    }];
    
    [self.talksButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.topView);
        make.leading.mas_equalTo(self.seeyouButton.mas_trailing).offset(50 * SCREENH6);
    }];
    
    [self.cameraButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-13 * SCREENH6);
        make.centerY.mas_equalTo(self.topView);
        make.width.mas_equalTo(25 * SCREENH6);
        make.height.mas_equalTo(21 * SCREENH6);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.makeFirendsButton);
        make.height.mas_equalTo(1 * SCREENH6);
        make.bottom.mas_equalTo(self.topView).offset(-3 * SCREENH6);
        make.width.mas_equalTo(45 * SCREENH6);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.oneLineView);
        make.centerX.mas_equalTo(self.seeyouButton);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.twoLineView);
        make.centerX.mas_equalTo(self.talksButton);
    }];
}


- (UIButton *)setupButtonWith:(NSString *)string{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:string forState:UIControlStateNormal];
    [button setTitleColor:BLACK_COLOR forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickTopButon:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    
    return button;
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = MAINCOLOR;
    lineView.hidden = YES;
    
    return lineView;
}


- (void)setupXieHouView{
    
    XiehouView * xiehouView = [[XiehouView alloc] initWithFrame:CGRectMake(0, 45 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT - 165 * SCREENH6)];
    
//    xiehouView.hidden = YES;
//    NSLog(@"===%@",self.xiehouListArray);
    xiehouView.modelArray = self.xiehouListArray;
    
    self.xiehouView = xiehouView;
    
    [self.view addSubview:self.xiehouView];

    
}

- (void)clickTopButon:(UIButton *)button{
    
    if (button == self.makeFirendsButton) {
        
        if (!self.makeFirendsButton.selected) {
            
            self.type = @"one";
            self.collectionView.hidden = NO;
            self.tableView.hidden = YES;
            self.cameraButton.hidden = NO;
            [self.cameraButton setImage:ImageNamed(@"相机") forState:UIControlStateNormal];
//            self.xiehouView.hidden = YES;
            [self.xiehouView removeFromSuperview];
            
            self.makeFirendsButton.selected = YES;
            self.oneLineView.hidden = NO;
            
            self.seeyouButton.selected = NO;
            self.twoLineView.hidden = YES;
            self.talksButton.selected = NO;
            self.threeLineView.hidden = YES;
        }
    }else if (button == self.seeyouButton){
        
        if (!self.seeyouButton.selected) {
            
            self.type = @"two";
            self.collectionView.hidden = YES;
            self.tableView.hidden = YES;
            self.cameraButton.hidden = YES;
//            self.xiehouView.hidden = NO;
//            self.xiehouView.modelArray = self.xiehouListArray;
            [self setupXieHouView];
            
            self.seeyouButton.selected = YES;
            self.twoLineView.hidden = NO;
            
            self.makeFirendsButton.selected = NO;
            self.oneLineView.hidden = YES;
            self.talksButton.selected = NO;
            self.threeLineView.hidden = YES;
        }
        
    }else if (button == self.talksButton){
        
        if (!self.talksButton.selected) {
            
            self.type = @"three";
            self.collectionView.hidden = YES;
            self.tableView.hidden = NO;
            self.cameraButton.hidden = NO;
            [self.cameraButton setImage:ImageNamed(@"发布1") forState:UIControlStateNormal];
//            self.xiehouView.hidden = YES;
            [self.xiehouView removeFromSuperview];
            self.talksButton.selected = YES;
            self.threeLineView.hidden = NO;
            
            self.makeFirendsButton.selected = NO;
            self.oneLineView.hidden = YES;
            self.seeyouButton.selected = NO;
            self.twoLineView.hidden = YES;
        }
        
    }
    
}


- (void)RefreshTalksData:(NSNotification *)noti{
    
    NSString * type = noti.userInfo[@"type"];
    
    self.typeTopic = type;
    
    [self loadTopicDataType:self.typeTopic];
}

- (void)clickCameraButton{
    
    HIDDEN_TAB;
    
    if([self.type isEqualToString:@"one"]){
        
        PushFriendsViewController * pushVC = [PushFriendsViewController new];
        
        [self.navigationController pushViewController:pushVC animated:YES];
        
    }else if([self.type isEqualToString:@"three"]){
        
        WeiteTieZiViewController * writeVC = [WeiteTieZiViewController new];
        
        writeVC.type = @"35";
        
        [self.navigationController pushViewController:writeVC animated:YES];
    }
   
    
    SHOW_TAB;
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
